package com.taobao.dingtalk.service.addresslist;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentCreateRequest;
import com.dingtalk.api.request.OapiV2DepartmentDeleteRequest;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.request.OapiV2DepartmentUpdateRequest;
import com.dingtalk.api.response.OapiV2DepartmentCreateResponse;
import com.dingtalk.api.response.OapiV2DepartmentDeleteResponse;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.dingtalk.api.response.OapiV2DepartmentUpdateResponse;
import com.taobao.api.ApiException;
import com.taobao.dingtalk.client.DingTalkClientToken;
import com.taobao.dingtalk.service.DingTalkAbstractService;

/**
 * 钉钉部门管理相关<br>
 * 使用时继承本类即可
 * @author nicolas
 *
 */
public class DingTalkDeptManageService extends DingTalkAbstractService {

	protected DingTalkDeptManageService(DingTalkClientToken dingTalkClientToken) {
		super(dingTalkClientToken);
	}
	/**
	 * 创建部门
	 * @param parentId
	 * @param outerDept
	 * @param hideDept
	 * @param createDeptGroup
	 * @param order
	 * @param name
	 * @param sourceIdentifier
	 * @param deptPermits
	 * @param userPermits
	 * @param outerPermitUsers
	 * @param outerPermitDepts
	 * @param outerDeptOnlySelf
	 */
	public void createDepartment(Long parentId, Boolean outerDept, Boolean hideDept, Boolean createDeptGroup, Long order, 
			String name, String sourceIdentifier, String deptPermits, String userPermits, String outerPermitUsers, 
			String outerPermitDepts, Boolean outerDeptOnlySelf) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/create");
			OapiV2DepartmentCreateRequest req = new OapiV2DepartmentCreateRequest();
			req.setParentId(parentId);
			req.setOuterDept(outerDept);
			req.setHideDept(hideDept);
			req.setCreateDeptGroup(createDeptGroup);
			req.setOrder(order);
			req.setName(name);
			req.setSourceIdentifier(sourceIdentifier);
			req.setDeptPermits(deptPermits);
			req.setUserPermits(userPermits);
			req.setOuterPermitUsers(outerPermitUsers);
			req.setOuterPermitDepts(outerPermitDepts);
			req.setOuterDeptOnlySelf(outerDeptOnlySelf);
			OapiV2DepartmentCreateResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	public void updateDepartment(Long deptId, Long parentId, Boolean outerDept, Boolean hideDept, Boolean createDeptGroup, Long order, 
			String name, String sourceIdentifier, String deptPermits, String userPermits, String outerPermitUsers, 
			String outerPermitDepts, Boolean outerDeptOnlySelf) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/update");
			OapiV2DepartmentUpdateRequest req = new OapiV2DepartmentUpdateRequest();
			req.setDeptId(deptId);
			req.setParentId(parentId);
			req.setOuterDept(outerDept);
			req.setHideDept(hideDept);
			req.setCreateDeptGroup(createDeptGroup);
			req.setOrder(order);
			req.setName(name);
			req.setSourceIdentifier(sourceIdentifier);
			req.setDeptPermits(deptPermits);
			req.setUserPermits(userPermits);
			req.setOuterPermitUsers(outerPermitUsers);
			req.setOuterPermitDepts(outerPermitDepts);
			req.setOuterDeptOnlySelf(outerDeptOnlySelf);
			req.setLanguage("zh_CN");
			req.setAutoAddUser(false);
//			req.setDeptManagerUseridList("manager200");
			req.setGroupContainSubDept(true);
			req.setGroupContainOuterDept(true);
			req.setGroupContainHiddenDept(true);
//			req.setOrgDeptOwner("100");
			OapiV2DepartmentUpdateResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除部门
	 * @param deptId
	 */
	public void removeDepartmentById(Long deptId) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/delete");
			OapiV2DepartmentDeleteRequest req = new OapiV2DepartmentDeleteRequest();
			req.setDeptId(deptId);
			OapiV2DepartmentDeleteResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取部门详情
	 * @param deptId
	 */
	public void getDepartmentById(Long deptId) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
			OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
			req.setDeptId(100L);
			req.setLanguage("zh_CN");
			OapiV2DepartmentGetResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询子部门列表
	 * @param deptId
	 * @param language 固定“zh_CN”，可不传
	 */
	public OapiV2DepartmentListsubResponse findSubDepartmentList(Long deptId, String language) {
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
			OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
			req.setDeptId(deptId);
			req.setLanguage("zh_CN");
			OapiV2DepartmentListsubResponse rsp = client.execute(req, dingTalkClientToken.getToken());
			System.out.println(rsp.getBody());
			return rsp;
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
